<template>
	<view>
		<cu-custom bgColor="bg-red" :isBack="true">		<block slot="backText"></block>
			<block slot="content">填写订单</block>
		</cu-custom>
		<view :class="(lifting_id==1 || lifting_id == 4)?'show':'hide'">
			<addressView ref="addressView"></addressView>
		</view>
		<view class="bg-white margin-lr margin-top" v-if="detail">
			<view class="cu-list menu">
				<view class="cu-item">
					<view class="content" v-if="detail.MemberApply">
						<text>酒库：</text>{{detail.MemberApply.name}}
						 <view>
							<text>负责人：</text> {{detail.MemberApply.username}}
						 </view>
					</view>
					<view class="action" v-if="detail.MemberWord">
						<view><text>网点：</text>{{detail.MemberWord.name}}</view>
						<view>
							<text>负责人：</text> {{detail.MemberWord.username}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="bg-white margin-lr margin-top">
			<view class="cu-bar bg-white solid-bottom margin-top">
				<view class="action"><text class=" text-bold text-lg">商品信息</text></view>
			</view>
			<view class="goods_list margin-lr" v-if="detail">
				<block v-for="(row,index) in detail.products" :key="index">
					<orderGoods :value="row"></orderGoods>
				</block>
			</view>
			<view class="cu-list menu">
				<view class="cu-item">
					<view class="content">备注</view>
					<view class="action">
						<input class="text-right" placeholder="请输入备注" v-model="formData.buyer_message" />
					</view>
				</view>
			</view>
		</view>

		<view class="bg-white margin-lr margin-top">
			<view class="cu-list menu">
				<view class="cu-item" v-if="detail && detail.coupons && detail.coupons.length > 0">
					<view class="content">优惠劵</view>
					<view class="action ">
						<view v-if="detail && detail.coupons && detail.coupons.length > 0" class="text-red">
							<picker @change="bindCouponsPickerChange" :value="coupons_index" :range="detail.coupons" range-key="title">
								<view class="uni-input">{{detail.coupons[coupons_index]?detail.coupons[coupons_index].title:'请选择优惠券'}}</view>
							</picker>
						</view>
						<view v-else class="text-gray">暂无可用优惠券</view>
					</view>
				</view>
				<view class="cu-item" v-if="detail && (detail.is_logistics == 1 || detail.pickup_point_config.buyer_self_lifting)">
					<view class="content">配送方式</view>
					<view class="action text-red">
						<picker @change="bindLiftingPickerChange" :value="lifting_index" :range="lifting_rank" range-key="title">
							<view class="uni-input">{{lifting_rank[lifting_index] ? lifting_rank[lifting_index].title:'未选择配送方式'}}</view>
						</picker>
					</view>
				</view>
				<view class="cu-item" v-if="detail && detail.pickup_point_config.buyer_self_lifting && lifting_id==2">
					<view class="content" style="min-width: 200rpx;">门店选择</view>
					<view class="action text-red text-right">
						<picker @change="bindPickUpPickerChange" :value="pickup_index" :range="detail.pickup_point_config.list" range-key="name">
							<view class="uni-input" v-if="detail.pickup_point_config.list[pickup_index]">{{detail.pickup_point_config.list[pickup_index].name}}-{{detail.pickup_point_config.list[pickup_index].address_name}}
								{{detail.pickup_point_config.list[pickup_index].address}}
							</view>
						</picker>
					</view>
				</view>
				<view class="cu-item" v-if="detail && detail.is_logistics == 1 && detail.is_logistics && lifting_id==1">
					<view class="content">快递选择</view>
					<view class="action text-red">
						<picker @change="bindLogisticsPickerChange" :value="logistics_index" :range="detail.company" range-key="title">
							<view class="uni-input">{{detail.company[logistics_index]?detail.company[logistics_index].title:''}}</view>
						</picker>
					</view>
				</view>
				<view class="cu-item" v-if="detail && detail.order_is_invoice == 1">
					<view class="content">开具发票</view>
					<view class="action">
						<invoiceView ref="invoiceView"></invoiceView>
					</view>
				</view>
				<view class="cu-item" v-if="detail && detail.order_is_invoice == 1 && invoice_select">
					<view class="content">发票类型</view>
					<view class="action">
						<radio-group class="block" @change="InvoiceTypeRadioChange">
							<view class="cu-form-group margin-top">
								<radio :class="invoice_index==index?'checked':''" :checked="invoice_index==index?true:false" v-for="(item,index) in detail.invoice.list"
								 :key="index" :value="index+''">{{item}}</radio>
							</view>
						</radio-group>
					</view>
				</view>
			</view>
		</view>

		<view class="bg-white margin-lr margin-top">
			<view class="cu-list menu">
				<view class="cu-item">
					<view class="content">商品总额</view>
					<view class="action text-price text-red">{{detail?detail.preview.product_money:0}}</view>
				</view>
				<view class="cu-item" v-if="couponsMoney>0">
					<view class="content">优惠金额</view>
					<view class="action  text-red">-<text class="text-price">{{couponsMoney}}</text></view>
				</view>
				<view class="cu-item" v-if="shippingMoney>0">
					<view class="content">快递费用</view>
					<view class="action text-price text-red">{{shippingMoney}}</view>
				</view>
				<view class="cu-item" v-if="detail && detail.order_is_invoice && invoiceTaxMoney>0">
					<view class="content">发票税费</view>
					<view class="action text-price text-red">{{invoiceTaxMoney}}</view>
				</view>
				<!-- <view class="cu-item" v-if="detail && detail.preview.marketing_money>0">
					<view class="content">活动优惠</view>
					<view class="action text-red">-<text class="text-price ">{{detail.preview.marketing_money}}</text></view>
				</view> -->
				<block v-if="detail && detail.point_config.is_open == 1 && detail.max_use_point">
					<view class="cu-item">
						<view class="content">积分抵现(剩余:{{detail.account.user_integral}})</view>
						<view class="action space-between">
							<input v-model="formData.point" type="number" class="text-right" style="width: 100px;" @input="pointChange" />
							<view class="text-blue sm margin-left-sm" @tap="allPoint">全部</view>
						</view>
					</view>
					<view class="cu-item">
						<view class="content"></view>
						<view class="action text-right">
							最多可用{{detail.max_use_point}}，抵现：{{pointMoney}}元
						</view>
					</view>
				</block>
			</view>
		</view>
		<view class="cu-bar bg-white tabbar border shop foot" v-if="detail">
			<view class="padding">
				实付款：<text class="text-price text-red">{{totalMoney}}</text>
				<block v-if="detail.preview.point>0">+{{detail.preview.point}}积分</block>
				<block v-if="detail.preview.whole_coupon>0">+{{detail.preview.whole_coupon}}秒杀券</block>
				<block v-if="detail.preview.thing_integral>0">+{{detail.preview.thing_integral}}好东西积分</block>
			</view>
			
			<block v-if="detail.preview.point > 0">
				<block v-if="detail.account.user_integral > detail.preview.point">
					<view class="submit" :class="'bg-'+theme" @tap="createOrder">提交订单(积分：{{detail.account.user_integral}})</view>
				</block>
				<block v-else>
					<view class="submit" :class="'bg-gray'">积分不足(剩余：{{detail.account.user_integral}})</view>
				</block>
			</block>
			<block v-else>
				<block v-if="detail.preview.whole_coupon > 0">
					<block v-if="detail.account.wholesale_coupon > detail.preview.whole_coupon">
						<view class="submit" :class="'bg-'+theme" @tap="createOrder">提交订单(秒杀券：{{detail.account.wholesale_coupon}})</view>
					</block>
					<block v-else>
						<view class="submit" :class="'bg-gray'">秒杀券不足(剩余：{{detail.account.wholesale_coupon}})</view>
					</block>
				</block>
				
				<block v-if="detail.preview.thing_integral > 0">
					<block v-if="detail.account.thing_integral > detail.preview.thing_integral">
						<view class="submit" :class="'bg-'+theme" @tap="createOrder">提交订单(好东西积分：{{detail.account.thing_integral}})</view>
					</block>
					<block v-else>
						<view class="submit" :class="'bg-gray'">好东西积分不足(剩余：{{detail.account.thing_integral}})</view>
					</block>
				</block>
				
				<view v-else class="submit" :class="'bg-'+theme" @tap="createOrder">提交订单</view>
			</block>
			
			
			
		</view>

		<foot :value="''"></foot>
	</view>
</template>

<script>
	import Foot from '@/components/default/foot.vue'
	import orderGoods from '@/components/shop/orderGoods.vue'
	import addressView from '@/components/shop/addressView.vue'
	import invoiceView from '@/components/shop/invoiceView.vue'
	import {
		mapState
	} from 'vuex'
	export default {
		components: {
			orderGoods,
			addressView,
			invoiceView,
			Foot
		},
		computed: mapState(['forcedLogin', 'hasLogin', 'channelLogin']),
		data() {
			return {
				detail: null,
				coupons_index: null, //优惠券选择
				lifting_index: 0, //配送选择
				lifting_id: 0,
				pickup_index: 0, //自提选择
				logistics_index: 0, //快递选择
				invoice_index: 0, //发票类型
				lifting_rank: [],
				productTotalMoney: 0, //商品总额
				couponsMoney: 0, //优惠金额
				shippingMoney: 0, //运费
				invoiceTaxMoney: 0, //税费
				totalMoney: 0,
				formData: {
					merchant_id: 0,
					is_channel: 0,
					address_id: 0,
					// coupon_id: 0,
					buyer_message: '',
					// company_id: 0,
					data: null,
					// pickup_id: 0,
					shipping_type: 1,
					type: '',
					point: 0,
					network_nid:0,
					store_nid:0,
					whole_coupon:0
				},
				invoice_select: null,
				loading: false,
				CLOUDPATH: this.$config.cloudPath,
				theme: this.$config.THEME(),
				wholesale_product_id: 0, //拼团id
				wholesale_id: 0,
				merchant_id: 0,
				pointMoney: 0,
				
			}
		},
		onLoad(options) {
			if (options.merchant_id) {
				this.formData.merchant_id = options.merchant_id
				this.merchant_id = options.merchant_id
			}
			this.formData.type = options.type
			this.formData.data = options.data
			if (!options.data) {
				this.$common.showErrorModal('商品信息有误')
				return
			}
			if (options.wholesale_product_id) this.wholesale_product_id = options.wholesale_product_id
			if (options.wholesale_id) this.wholesale_id = options.wholesale_id
			
		},
		onShow() {
			if (this.$common.loginAuth(true)) {
				var address_select = this.$common.getCache('address_select')
				if (address_select) {
					this.$nextTick((r) => {
						this.$refs.addressView.setAddress(address_select)
						this.$common.rmCache('address_select')
					})
				}
				var invoice_select = this.$common.getCache('invoice_select')
				if (invoice_select) {
					this.$nextTick((r) => {
						this.$common.rmCache('invoice_select')
						this.$refs.invoiceView.setInvoice(invoice_select)
						this.invoice_select = invoice_select
						this.mathInvoiceTaxMoney()
					})
				}
			}
			this.getPreview()
		},
		methods: {
			getPreview() {
				uni.showLoading({
					title: '加载中...'
				})
				
				let params = {
					mer_id: this.merchant_id,
					type: this.formData.type,
					data: this.formData.data,
					is_channel: this.formData.is_channel,
					address_id: 0
				}
				
				if (this.$common.getCache('address_select') != null) {
					let address = this.$common.getCache('address_select')
					params.address_id = address.id
				}
				
				if (this.wholesale_product_id) params.wholesale_product_id = this.wholesale_product_id
				if (this.wholesale_id) params.wholesale_id = this.wholesale_id
				this.$http
					.get('order_preview', params)
					.then((response) => {
						this.detail = response.data.data
						if (this.detail.is_logistics == 1) {
							this.lifting_rank.push({
								id: 1,
								title: '物流配送'
							})
						}
						if (this.detail.pickup_point_config.buyer_self_lifting == 1) {
							this.lifting_rank.push({
								id: 2,
								title: '门店自提'
							})
						}
						if (this.detail.is_delivery_shop == 1) {
							this.lifting_rank.push({
								id: 4,
								title: '门店配送'
							})
						}
						if (this.detail.is_logistics == 1 || this.detail.is_delivery_shop == 1) {
							if (this.detail.address) {
								this.$refs.addressView.setAddress(this.detail.address)
							}
						}
						this.lifting_id = this.lifting_rank.length > 0 ? this.lifting_rank[0].id : 0
						console.log(this.lifting_id)

						this.$common.rmCache('cart_num')
						this.mathTotalProductMoney()
						this.mathShippingMoney()
					})
					.catch((response) => {
						console.log(response)
					})
			},
			bindCouponsPickerChange(e) {
				this.coupons_index = e.detail.value
				this.couponsMoney = this.detail.coupons[this.coupons_index].money
				this.mathShippingMoney()
				console.log(this.couponsMoney)
			},
			bindLiftingPickerChange(e) {
				this.lifting_index = e.detail.value
				this.lifting_id = this.lifting_rank[this.lifting_index].id
				this.mathShippingMoney()
			},
			bindPickUpPickerChange(e) {
				this.pickup_index = e.detail.value
			},
			bindLogisticsPickerChange(e) {
				this.logistics_index = e.detail.value
			},
			InvoiceTypeRadioChange(e) {
				this.invoice_index = e.detail.value
				this.mathInvoiceTaxMoney()
			},
			mathTotalProductMoney() {
				let productTotalMoney = 0
				for (let i in this.detail.products) {
					productTotalMoney += this.detail.products[i].product_money * this.detail.products[i].num
				}
				this.productTotalMoney = productTotalMoney.toFixed(2)
				this.mathTotalMoney()
			},
			mathShippingMoney() {
				if (this.lifting_index == 0)
					this.shippingMoney = this.detail.preview.shipping_money
				else this.shippingMoney = 0
				this.mathTotalMoney()
			},
			mathInvoiceTaxMoney() {
				let invoiceTaxMoney = this.detail.invoice.order_invoice_tax * this.productTotalMoney / 100
				this.invoiceTaxMoney = invoiceTaxMoney.toFixed(2)
				this.mathTotalMoney()
			},
			mathTotalMoney() {
				let totalMoney = (parseFloat(this.detail.preview.product_money) + parseFloat(this.shippingMoney) +
						parseFloat(this.invoiceTaxMoney) -
						parseFloat(this.couponsMoney)) // - parseFloat(this.detail.preview.marketing_money)
					-
					this.pointMoney
					console.log(this.pointMoney)
				this.totalMoney = totalMoney.toFixed(2)
			},
			createOrder() {
				if (this.loading) return
				if (this.coupons_index != null && this.coupons_index >= 0) {
					this.formData.coupon_id = this.detail.coupons[this.coupons_index].id
				}
				// console.log(this.lifting_rank);return
				if (this.lifting_index >= 0 ) {
					this.formData.shipping_type = this.lifting_rank[this.lifting_index].id
				}

				if (this.formData.shipping_type == 1) {
					
					if (this.detail.is_logistics && this.detail.company.length > 0) {
						//可以选择快递公司
						if (this.logistics_index>=0)
							this.formData.company_id = this.detail.company[this.logistics_index].id
						else if (this.detail.pickup_point_config.buyer_self_lifting) {
							this.$common.showError('请选择物流方式')
							return
						}
					}

				} else if (this.formData.shipping_type == 2) {
					if (this.detail.pickup_point_config.buyer_self_lifting == 1) {
						//可以选择门店提货
						if (this.pickup_index >= 0) {
							this.formData.pickup_id = this.detail.pickup_point_config.list[this.pickup_index].id
						} else {
							this.$common.showError('请选择自提的门店')
							return
						}
					}
				}
				if (this.formData.shipping_type == 1 || this.formData.shipping_type == 4) {
					let address = this.$refs.addressView.getAddress()
					if (!address) {
						this.$common.showError('请选择收货地址')
						return
					}
					this.formData.address_id = address.id
				}
				if (this.detail.order_is_invoice && this.invoice_select) {
					this.formData.invoice_id = this.invoice_select.id
					this.formData.invoice_content = this.detail.invoice.list[this.invoice_index]
				}
				console.log(this.formData)
				// return
				this.formData.use_point = this.formData.point
				if(this.detail.MemberApply){
					this.formData.store_nid=this.detail.MemberApply.id
				}
				if(this.detail.MemberWord){
					this.formData.network_nid=this.detail.MemberWord.id
				}
				
				this.loading = true
				uni.showLoading({
					title: '提交中...'
				})
				if (this.wholesale_product_id) {
					this.formData.wholesale_product_id = this.wholesale_product_id
				}
				if (this.wholesale_id) this.formData.wholesale_id = this.wholesale_id

				this.$http
					.post('order_create', this.formData)
					.then((response) => {
						this.loading = false
						uni.hideLoading()
						var order_id = response.data.data.id
						this.$common.showSuccess('提交成功', function() {
							uni.redirectTo({
								url: "/pages/member/wallet/pay?id=" + order_id
							})
						})
					})
					.catch((response) => {
						this.loading = false
						console.log(response)
						uni.hideLoading()
					})
			},
			pointChange(e) {
				var point  = e.detail.value 
				if (point > this.detail.account.user_integral) {
					point = this.detail.account.user_integral
				}
				if (point > this.detail.max_use_point)
					point = this.detail.max_use_point
				this.$nextTick((r) => {
					this.formData.point = point
					this.mathPointMoney()
					this.$forceUpdate()
				})
			},
			allPoint() {
				var point = this.detail.account.user_integral
				console.log(this.formData.point + '-' + this.detail.max_use_point)
				if (point > this.detail.max_use_point) {
					point = this.detail.max_use_point
				}
				console.log(point)
				this.$nextTick(()=>{
					this.formData.point = point
					console.log(this.formData.point + '-' + this.detail.max_use_point)
					this.mathPointMoney()
					this.$forceUpdate()
				})
			},
			mathPointMoney() {
				var value = (this.detail.point_config.convert_rate * this.formData.point)
				if(value>this.detail.preview.product_money){
					this.formData.point = this.detail.preview.product_money / this.detail.point_config.convert_rate
				}
				value = (this.detail.point_config.convert_rate * this.formData.point)
				this.pointMoney = value.toFixed(2)
				if (this.pointMoney > this.detail.preview.product_money) {
					console.log(this.detail.preview.product_money)
					// this.formData.point = this.detail.preview.product_money / this.detail.point_config.convert_rate
					value = (this.detail.point_config.convert_rate * this.formData.point)
					this.pointMoney = value.toFixed(2)
				}
				this.mathTotalMoney()
			}
		}
	}
</script>

<style>
	.hide {
		display: none;
	}
</style>
